package com.gis.visualization.controller;


import com.gis.visualization.config.result.R;
import com.gis.visualization.entity.HistoryFigure;
import com.gis.visualization.entity.vo.EventQueryVo;
import com.gis.visualization.entity.vo.FigureQueryVo;
import com.gis.visualization.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;


/**
 * <p>
 * 历史人物表 前端控制器
 * </p>
 *
 * @author hqz
 * @since 2021-02-23
 */
@RestController
@RequestMapping("/visualization/historyfigure")
//@CrossOrigin
@Api(tags = "历史人物管理")
public class HistoryFigureController {

    @Autowired
    private HistoryFigureService figureService;
    @Autowired
    private FigureLocationService locationService;
    @Autowired
    private FigureLocationDescService locationDescService;
    @Autowired
    private HistorySummaryService summaryService;
    @Autowired
    private OssService ossService;

    @ApiOperation("添加历史人物")
    @PostMapping("addFigureInfo")
    public R addFigureInfo(@RequestBody HistoryFigure historyFigure) {
        figureService.save(historyFigure);
        return R.ok().data("figureId", historyFigure.getId()).message("历史人物基本信息添加成功");
    }

    @ApiOperation("获取历史人物基本信息")
    @GetMapping("getFigureInfo/{figureId}")
    public R getEventInfo(@PathVariable String figureId) {
        HistoryFigure figure = figureService.getById(figureId);
        return R.ok().data("figure", figure).message("历史人物基本信息获取成功");
    }

    @ApiOperation("更新历史人物基本信息")
    @PostMapping("updateFigureInfo")
    public R updateFigureInfo(@RequestBody HistoryFigure historyFigure) {
        figureService.updateById(historyFigure);
        return R.ok().message("历史人物基本信息更新成功");
    }

    //    条件查询带分页的方法
    @ApiOperation(value = "人物条件查询带分页的方法")
    @PostMapping("pageFigureCondition/{current}/{limit}")
    public R pageFigureCondition(@ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current,
                                 @ApiParam(name = "limit", value = "单页数据个数", required = true) @PathVariable long limit,
                                 @ApiParam(name = "FigureQueryVo", value = "查询条件对象", required = false)
                                 @RequestBody(required = false) FigureQueryVo figureQueryVo) {
        Map<String, Object> map = figureService.pageFigureCondition(current, limit, figureQueryVo);
        return R.ok().data(map);
    }

    @ApiOperation("删除历史人物")
    @DeleteMapping("deleteFigure/{figureId}")
    public R deleteFigure(@PathVariable String figureId) {
        figureService.deleteFigure(figureId);
        return R.ok().message("历史人物删除成功");
    }

    @ApiOperation("从excel中读取人物进行存储")
    @PostMapping("figureFromExcel")
    public R figureFromExcel(MultipartFile file) {
        if (file == null) {
            return R.error().message("文件为空，请选择excel文件再试");
        }
        figureService.figureFromExcel(file, figureService, locationService, locationDescService, summaryService, ossService);
        return R.ok().message("成功从excel中读取历史人物存储到数据库中");
    }
}

